program polnij_perebor;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Math,
  ulkJSON in 'ulkJSON.pas';

type
  item = record
    name : string[50];
    weight : integer;
    price : integer;
    taken : boolean;
    func: real;
  end;

var
  items : Array of item;
  size : Integer;
  i : integer;
  MaxPrice : integer;
  currprice : integer;
  currweight:integer;
  ans:array[0..25] of boolean;
  js:TlkJSONobject;
  f : Text;
  jsn : String;
  s : String;
begin
  // get stupid JSON
  assignfile(f, '../pool.js');
  reset(f);
  jsn:= '{"items":';
  while not eof(f) do
  begin
    readln(f, s);
    jsn:=jsn+s;
  end;
  jsn:=jsn+'}';
  js := TlkJSON.ParseText(jsn) as TlkJSONobject;

  writeln(((js.Field['items'] as TlkJSONList).child[0] as TlkJSONobject).field['price'].value);
  // parse stupid JSON
  for i := 0 to (js.Field['items'] as TlkJSONList).count - 1 do
  begin
    setlength(items, length(items)+1);
    items[i].name := ((js.Field['items'] as TlkJSONList).child[i] as TlkJSONobject).getstring('name');
    items[i].weight := strtoint(((js.Field['items'] as TlkJSONList).child[i] as TlkJSONobject).field['weight'].value);
    items[i].price := strtoint(((js.Field['items'] as TlkJSONList).child[i] as TlkJSONobject).field['price'].value);
    items[i].func := -1;
    items[i].taken := false;
  end;
  js.Free;
  size := 4000;

  //random items generation
  {for i:=0 to 21 do
  begin
    setlength(items, length(items)+1);
    items[i].name := inttostr(i);
    items[i].weight := random(30)+1;
    items[i].price := random(30)+1;
    items[i].taken := false;
  end;  }

  maxprice := 0;

  for i:=0 to (length(items)-1) do
  writeln(inttostr(i)+': '+inttostr(items[i].weight)+' '+inttostr(items[i].price));

  while (true) do
  begin
    i:=0;
    while (items[i].taken) do
    begin
      items[i].taken := false;
      inc(i);
    end;
    if (i = length(items)) then
      break;
    items[i].taken := true;
    currweight := 0;
    currprice := 0;
    for i := 0 to (length(items)-1) do
    begin
      if items[i].taken then
      begin
        currweight := currweight + items[i].weight;
        if currweight > size then
          break
        else
          currprice := currprice + items[i].price;
      end;
    end;
    if currprice > maxprice then
    begin
    //remember combination
      maxprice := currprice;
      for i:=0 to (length(items)-1) do
        if items[i].taken then
          ans[i] := true
        else
          ans[i] := false;
    end;
  end;

 { writeln('max:'+inttostr(maxprice));
  for i:=0 to (length(items)-1) do
        if ans[i] then
          writeln(i);
  readln;   }

  assignfile(f, '../knapsack.js');
  rewrite(f);
  s:='['+#13#10;
  for i:=0 to length(items)-1 do
    if ans[i] then
    begin
      s:=s+'{"name": "'+items[i].name+'", ';
      s:=s+'"weight": '+inttostr(items[i].weight) + ', ';
      if i<>length(items)-1 then
        s:=s+'"price": '+inttostr(items[i].price) + '},'+#13#10
      else
        s:=s+'"price": '+inttostr(items[i].price) + '}'+#13#10
    end;
  s:=s+']';
  write(f, s);
  closefile(f);
end.

 